package com.wnx.job.modules.jms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.domain.CascadeNode;
import com.wnx.job.domain.ProfessionNode;
import com.wnx.job.modules.jms.mapper.JmsProfessionMapper;
import com.wnx.job.modules.jms.model.JmsProfession;
import com.wnx.job.modules.jms.service.JmsProfessionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class JmsProfessionServiceImpl extends ServiceImpl<JmsProfessionMapper, JmsProfession> implements JmsProfessionService {
    @Resource
    private JmsProfessionMapper professionMapper;

    /**
     * 新增专业信息
     * @param tbProfession
     */
    @Override
    public void saveProfession(JmsProfession tbProfession) {
        JmsProfession x = findByName(tbProfession.getName());
        if (x != null){
            Asserts.fail("专业名称不能重复！");
        }

        professionMapper.insert(tbProfession);

    }
    /**
     * 删除专业信息
     * @param id
     */
    @Override
    public void deleteProfession(Long id) {
        //校验，删除的节点下面无子节点
        long count = this.findCountByParentId(id);
        if (count > 0){Asserts.fail("该节点下还存在子节点，请先删除子节点");}

        professionMapper.deleteById(id);
    }

    /**
     * 修改专业信息
     * @param tbProfession
     */
    @Override
    public void updateProfession(JmsProfession tbProfession) {
        JmsProfession x = findByName(tbProfession.getName());
        if (x != null){
            Asserts.fail("专业名称不能重复！");
        }

        professionMapper.updateById(tbProfession);
    }

    /**
     * 获取专业树
     * @return
     */
    @Override
    public List<CascadeNode> getTree() {
        //调用持久层，查询全部专业集合。
        List<JmsProfession> professionList = professionMapper.selectList(null);



        // 专业集合转专业树
        List<ProfessionNode> result = professionList.stream()
                .filter(profession -> profession.getParentId().equals(0L))
                .map(profession -> buildTree(profession, professionList))
                .collect(Collectors.toList());

        //转成级联DTO
        return convert(result);

    }

    /**
     * 构建树形结构
     * @param profession
     * @param professionList
     * @return
     */
    private ProfessionNode buildTree(JmsProfession profession, List<JmsProfession> professionList) {
        ProfessionNode node = new ProfessionNode();
        BeanUtils.copyProperties(profession,node);
        List<ProfessionNode> children =
                professionList.stream()
                        .filter(subProfession -> subProfession.getParentId().equals(profession.getId()))
                        .map(subPermission -> buildTree(subPermission, professionList))
                        .collect(Collectors.toList());

        node.setChildren(children);

        return node;
    }
    /**
     * 实体转DTO
     * @param dictTree
     * @return
     */
    private List<CascadeNode> convert(List<ProfessionNode> dictTree) {

        List<CascadeNode> volist = new ArrayList<>();
        dictTree.forEach(m -> {
            CascadeNode vo = new CascadeNode();
            vo.setLabel(m.getName());
            vo.setValue(m.getId());
            List<ProfessionNode> sonProfessionList = m.getChildren();
            if (sonProfessionList != null && sonProfessionList.size() > 0) {
                vo.setChildren(convert(sonProfessionList));
            }
            volist.add(vo);
        });
        return volist;
    }



    /**
     * 根据父ID查询专业数量
     * @param id
     * @return
     */
    private long findCountByParentId(Long id){
        return professionMapper.selectCount(new QueryWrapper<JmsProfession>().lambda().eq(JmsProfession::getParentId, id));
    }
    /**
     * 根据专业名查询专业
     * @param name
     * @return
     */
    @Override
    public JmsProfession findByName(String name){
        return professionMapper.selectOne(new QueryWrapper<JmsProfession>().lambda().eq(JmsProfession::getName, name));
    }
}
